'use strict';
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;

var server_list = require("../server.json")

var { db_url, auth } = require('../config.json')

var db = new MongoClient(db_url);
var connections = db.connect();

/* GET home page. */
router.get('/', function (req, res) {
    res.json({ "info": "hello" });
});

router.post('/:auth', function (req, res) {
    if (req.params.auth != auth) {
        res.json({ "info": "no auth" })
    }
    var msg = JSON.parse(message)
    var server_id = ""
    //if (Date.now() % 10 != 0)
    let temp_date = new Date()
    msg["createdAt"] = temp_date
    msg["locale"] = temp_date.toLocaleTimeString("en-SG", {
        timeZone: 'Asia/Singapore'
    })
    if (msg.token && server_list[msg.token]) {
        server_id = server_list[msg.token]["name"]
        server_list[msg.token]["status"] = msg
        //console.log(server_list[msg.token])
        connections.then(
            async (db) => {
                const dbo = db.db("serverstatus");
                //server
                var server_collection = dbo.collection(server_id);
                var _temp = await server_collection.insertOne(msg)
            }
        )
    }

});

module.exports = router;